<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top:5%;height: 85%;overflow-y: hidden">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="角色分配" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="!roleDisabled" >
          <a-select
            mode="multiple"
            style="width: 100%"
            placeholder="请选择用户角色"
            v-model="selectedRole">
            <a-select-option v-for="(role,roleindex) in roleList" :key="roleindex.toString()" :value="role.id">
              {{ role.roleName }}
            </a-select-option>
          </a-select>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import {addTeamrole,duplicateCheck } from '@/api/team'
  import {userall} from '@/api/team'
  import {addUser,editUser,queryUserRole,queryall,checkUsername,addUserrole,editUserrole } from '@/api/api'
  import RoleModal from './RoleModal'


  export default {
    name: "RoleassignModal",
    components: {
      RoleModal
    },
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules:{
          roleName:{
            rules: [
              { required: true, message: '请选择角色!' },
              { min: 0, max: 126, message: '长度在 2 到 30 个字符', trigger: 'blur' }
            ]}
        },
        roleDisabled: false, //是否是角色维护调用该页面
        roleId: '',
        roleList:[],
        specialtyList:[],
        selectedRole:[],
        form:this.$form.createForm(this),
        picUrl: ""
      }
    },
    created () {
    },
    methods: {
      refresh () {
        this.userId=""
      },


      add () {
        this.picUrl = "";
        this.refresh()
        this.edit({});
      },
      edit (record) {
        /*      let that = this;*/
        this.form.resetFields();

        this.visible = true;
        let that=this
        that.initialRoleList();
        if(record.hasOwnProperty("id")){
          that.loadUserRoles(record.id);
          this.picUrl = "Has no pic url yet";
        }
        that.userId = record.id;
        that.visible = true;
        this.model = Object.assign({}, record);
        // that.model = Object.assign({}, this.model);
        that.$nextTick(() => {
          that.form.setFieldsValue(pick(this.model))
        });

      },
      initialRoleList(){
        queryall().then((res)=>{
          if(res.success){
            this.roleList = res.result;
          }else{
            console.log(res.message);
          }
        });
      },
      loadUserRoles(userid){
        queryUserRole({userid:userid}).then((res)=>{
          if(res.success){
            this.selectedRole = res.result;
          }else{
            console.log(res.message);
          }
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let avatar = that.model.avatar;
            let formData = Object.assign(this.model, values);
            formData.selectedroles = this.selectedRole.length>0?this.selectedRole.join(","):'';
            //formData.avatar = avatar;
            let obj;
            console.log(formData)
            if(!this.model.id){
              obj=addUserrole(formData);
            }else{
              obj=editUserrole(formData);
            }
            obj.then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      }

    }
  }
</script>

<style scoped>

</style>